<?

class request
{
	public static function get_action()
	{
		return $_GET['action'] ? str_replace('/', '_', trim($_GET['action'], '/ ')) : 'index';
	}

	public static function get_method()
	{
		return $_SERVER['REQUEST_METHOD'];
	}

	public static function get_accept()
	{
		$accept = trim($_SERVER['HTTP_ACCEPT'], ';');
		return explode(', ', $accept);
	}

	public static function get( $name, $default = null )
	{
		if ( !array_key_exists($name, $_REQUEST) ) return $default;
		return $_REQUEST[$name];
	}

	public static function get_int( $name, $default = 0 )
	{
		if ( !array_key_exists($name, $_REQUEST) ) return $default;
		return (int)$_REQUEST[$name];
	}

	public static function get_float( $name, $default = 0.0 )
	{
		if ( !array_key_exists($name, $_REQUEST) ) return $default;
		return (float)$_REQUEST[$name];
	}

	public static function get_string( $name )
	{
		return (string)$_REQUEST[$name];
	}

	public static function get_current_url( $add_params = array() )
	{
		$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

		if ( $add_params )
		{
			$d = parse_url($url);
			if ( $d['query'] ) parse_str($d['query'], $query);

			foreach ( $add_params as $k => $v )
			{
				if ( in_array($k, array('host', 'path')) )
					$d[$k] = $v;
				else
					$query[$k] = $v;
			}

			$url = 'http://' . $d['host'] . $d['path'] . '?' . http_build_query($query);
		}

		return $url;
	}

	public static function get_referrer()
	{
		return $_SERVER['HTTP_REFERER'];
	}

	public static function is_ajax()
	{
		return (bool)$_SERVER['HTTP_X_REQUESTED_WITH'];
	}

	public static function is_dialog()
	{
		return (bool)$_REQUEST['DIALOG'];
	}
	
	public static function get_all()
	{
		return array_merge($_POST, $_GET);
	}
}